import { env } from "../../../utils/env";
import { pointsActivityEscalationadd } from "../../../server/api";
import { debounce } from "../../../server/shake";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    fileList: [], //需要上传图片的列表
    textvalue: "", //绑定文本域的值
    radio: "0",
    imgnum: "0",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},
  //图片上传
  afterRead(event) {
    const { file } = event.detail; //获取所需要上传的文件列表
    let that = this;
    file.forEach((file) => {
      wx.uploadFile({
        filePath: file.tempFilePath,
        name: "file",
        url: `${env.baseURL}/sys/common/upload`,
        header: {
          "X-Access-Token": wx.getStorageSync("token") || "",
          "content-type": "multipart/form-data",
          accept: "application/json",
        },
        success: async function (e) {
          const imgInfo = JSON.parse(e.data);
          const arr = [];
          arr.push({
            ...file,
            url: imgInfo.message.save,
            paths: imgInfo.message.savePath,
          });
          that.setData({ fileList: [...that.data.fileList, ...arr] });
          that.setData({ imgnum: that.data.fileList.length });
        },
      });
    });
    wx.hideLoading();
  },
  //删除图片
  deleteImg(data) {
    const idx = data.detail.index;
    this.data.fileList.splice(idx, 1);
    this.setData({
      fileList: this.data.fileList,
      imgnum: this.data.fileList.length,
    });
  },

  shangbao: debounce(async function () {
    if (this.data.fileList.length > 0) {
      const brr = [];
      this.data.fileList.map((res) => {
        brr.push(res.paths);
      });
      const params = {
        picturePaths: brr,
        sources: this.data.radio,
        remark: this.data.textvalue,
      };

      const data = await pointsActivityEscalationadd(params);
      if (data.code == 200) {
        wx.showToast({
          title: "上报成功",
          icon: "success",
        });
        setTimeout(() => {
          wx.navigateBack(-1);
        }, 1000);
      }
    } else {
      wx.showToast({
        title: "请上传图片",
        icon: "error",
      });
    }
  }, 3000),

//   async shangbao()
// {
//   if (this.data.fileList.length > 0) {
//     const brr = [];
//     this.data.fileList.map((res) => {
//       brr.push(res.paths);
//     });
//     const params = {
//       picturePaths: brr,
//       sources: this.data.radio,
//       remark: this.data.textvalue,
//     };

//     const data = await pointsActivityEscalationadd(params);
//     if (data.code == 200) {
//       wx.showToast({
//         title: "上报成功",
//         icon: "success",
//       });
//       // setTimeout(() => {
//       //   wx.navigateBack(-1);
//       // }, 1000);
//     }
//   } else {
//     wx.showToast({
//       title: "请上传图片",
//       icon: "error",
//     });
//   }
// },

  //取消
  cancel() {
    wx.navigateBack(-1);
  },
  //活动来源
  onChange(e) {
    this.setData({ radio: e.detail });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
